#include "mcc_generated_files/mcc.h"
#include "stdarg.h" 

//20190215  merge timer ping and PIR trigger.
#define VERSION_NUM "V1.3" 


char line[MAXCMD];
int icmd=0;

CMDLINE cmdline; 

#if 0
int printf(int,...)
{
   return 1;
}
#endif

volatile uint16_t eeprom_add;
volatile uint8_t eeprom_val;

T_CONFIG ee_config;
U_UINT32_UINT8   uint32_uint8;

void ee_config_write(uint8_t addr)
{
    uint8_t i;
    switch(addr)
    {
        case 0:
            uint32_uint8.value_uint32 = ee_config.devaddr;
            for(i = 0;i<4;i++)
             DATAEE_WriteByte(addr+i, uint32_uint8.value_uint8[i]);
            break;
        case 4:
            for(i = 0;i<16;i++)           
            DATAEE_WriteByte(addr+i, ee_config.nwkskey[i]);
            break;
        case 20:
            for(i = 0;i<16;i++)           
            DATAEE_WriteByte(addr+i, ee_config.appskey[i]);
            break;
        default:
            break;
    }
}

void ee_config_read(uint8_t addr)
{
    uint8_t i;
    switch(addr)
    {
        case 0:
            for(i = 0;i<4;i++)
            uint32_uint8.value_uint8[i] = DATAEE_ReadByte(addr+i);
            ee_config.devaddr = uint32_uint8.value_uint32;  
            break;
        case 4:
            for(i = 0;i<16;i++)           
            ee_config.nwkskey[i] = DATAEE_ReadByte(addr+i);
            break;
        case 20:
            for(i = 0;i<16;i++)           
            ee_config.appskey[i] = DATAEE_ReadByte(addr+i);
            break;
        default:
            break;
    }
}

int ACB2_ECL_config_test( int keyc, char *keyv[], int keyl[] )
{	
    int retval=0;
    uint16_t  i;     
    if(keyc>=2)
    { 
        retval += get_u32( keyv[0], keyl[0], &eeprom_add, 0, 4096, 1 );
        retval += get_u32( keyv[1], keyl[1], &eeprom_val, 0, 256, 1 );
        if(retval==0) 
        {   
            printf("write_ee:");
            DATAEE_WriteByte(eeprom_add, eeprom_val);
            printf("%x \r\n", eeprom_val);
        }
    }
    else if(keyc>=1)
    {
        retval += get_u32( keyv[0], keyl[0], &eeprom_add, 0, 4096, 1 );
        if(retval==0) 
        {   
            eeprom_val =  DATAEE_ReadByte(eeprom_add);
            printf("read_ee at 0x%x:0x%x\r\n", eeprom_add, eeprom_val); 
        }
    }    
    return (retval);
}
	
int Lora_ECL_Data( int keyc, char *keyv[], int keyl[] )
{
		
	
	int iret, retval, blknum;
  uint32_t ch, value;
  retval=0;
  char temp[16];
  int lens;
  if(keyc>=1)
{ 
    retval += get_u32( keyv[0], keyl[0], &temp[0], 0, 256, 1 );
    retval += get_u32( keyv[1], keyl[1], &temp[1], 0, 256, 1 );
    if(retval==0) 
	{
        
       printf("action:%d %d\r\n", temp[0], temp[1]);
     //  lens = sprintf(temp, "%d", ch );
       //lens = IntToChar(ch, (uint8_t)temp, 10);
       //if(lens > 0)
       
       LORAWAN_Send(UNCNF, 2, temp, 4);
       //else
       //LORAWAN_Send(UNCNF, 2, "9999", 4);
				    //Motor_action_rotate(ch);
						//Coil_lock_action(ch, 300);
						//printf("motor action ch:%d \r\n", ch);
	 retval=CMD_SUCCESS;
    }
  } 
else retval=CMD_TOO_FEW_ARGS;
 
return(retval);
}

int Lora_ECL_TxPower( int keyc, char *keyv[], int keyl[] )
{	
    uint8_t txpower_value=0;
    LorawanError_t lora_ret;
    int retval=0;
    uint8_t temp[1];
    if(keyc>=1)
    { 
        retval += get_u32( keyv[0], keyl[0], &temp[0], 0, 256, 1 );
        if(retval==0) 
        {
            lora_ret =  LORAWAN_SetTxPower(temp[0]);
            if(OK != lora_ret)
            {
                printf("SetTxPower Err:%d\r\n", lora_ret);
            }
        }
    }
    txpower_value = LORAWAN_GetTxPower();
    printf("GetTxPower:%d\r\n", txpower_value);

    return(retval);
}

int Lora_ECL_config_devaddr( int keyc, char *keyv[], int keyl[] )
{
    uint8_t i=0;

    if(keyc>=1)
    { 
        ee_config.devaddr = Hex2Dec_N(keyv[0], keyl[0], 1);
        ee_config_write(devaddr_a);
        printf("set devaddr ");
    }
    else
    {
        printf("get devaddr ");
    }
    ee_config_read(devaddr_a);
    uint32_uint8.value_uint32 = ee_config.devaddr;
    for(i=0;i<4;i++)
    printf("%02x", uint32_uint8.value_uint8[3-i]);
    printf("\r\n");
}

//write/read nwkskey
int Lora_ECL_config_nwkskey( int keyc, char *keyv[], int keyl[] )
{
    uint8_t i=0;
    if(keyc>=1)
    { 
        for(i=0;i<16;i++)
        ee_config.nwkskey[i] = Hex2Dec_N(keyv[0]+ i*2, 2, 1);
        ee_config_write(nwkskey_a);       
        printf("set nwkskey ");        
    }
    else
    {
        printf("get nwkskey ");
    }
    ee_config_read(nwkskey_a);
    for(i=0;i<16;i++)
    printf("%02x", ee_config.nwkskey[i]);
    printf("\r\n");
}

int Lora_ECL_config_appskey( int keyc, char *keyv[], int keyl[] )
{
    uint8_t i=0;
    if(keyc>=1)
    { 
        for(i=0;i<=16;i++)
        ee_config.appskey[i] =  Hex2Dec_N(keyv[0]+i*2, 2, 1);
        ee_config_write(appskey_a);       
        printf("set appskey ");        
    }
    else
    {
        printf("get appskey ");
    }
    ee_config_read(appskey_a);
    for(i=0;i<16;i++)
    printf("%02x", ee_config.appskey[i]);
    printf("\r\n");
}

int Lora_ECL_config_deviceeui( int keyc, char *keyv[], int keyl[] )
{
    uint8_t value[8],i;
    LORAWAN_GetDeviceEui(value);
    printf("get DeviceEui:0x");
    for(i=0;i<8;i++)
    printf("%02x", value[i]);
    printf("\r\n");
}
int Lora_ECL_config_display( int keyc, char *keyv[], int keyl[] )
{
   printf("Firmware Ver:%s\r\n", VERSION_NUM); 
   Lora_ECL_config_devaddr(0, NULL, NULL);
   Lora_ECL_config_nwkskey(0, NULL, NULL);
   Lora_ECL_config_appskey(0, NULL, NULL);
   //Lora_ECL_config_deviceeui(0, NULL, NULL);
}

const CMDS cmds[] = {
 
  { "wr",     	"read/write one byte",		     cmds,            NULL,          ACB2_ECL_config_test },
  { "ac",     	"Get/Set datas",                 cmds,            NULL,         Lora_ECL_Data },
  { "tp",     	"Get/Set Tx Power Level(5-10), high mean low power",		 cmds,            NULL,      Lora_ECL_TxPower },
  { "devaddr",     	"Get/Set devAddr",		 cmds,            NULL,      Lora_ECL_config_devaddr }, 
  { "nwkskey",     	"Get/Set nwkSKey",		 cmds,            NULL,      Lora_ECL_config_nwkskey },
  { "appskey",     	"Get/Set appSKey",		 cmds,            NULL,      Lora_ECL_config_appskey },
  { "sconfig",     	"sconfig",               cmds,            NULL,      Lora_ECL_config_display },
  {  NULL,       NULL,                         NULL,            NULL,            NULL },
};

const CMDS config_cmds[] = {
  { "config",   "config block management",     top,             config_cmds,   NULL },
  {  NULL,       NULL,                         NULL,            NULL,          NULL },
};

const CMDS top[] = {
  { "Lora",     "Lora Controller Debugger",  NULL,            cmds,          NULL },
  {  NULL,       NULL,                         NULL,            NULL,          NULL },
};
